//
//  CommunityViewController.m
//  Journey
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CommunityViewController.h"
#import "HotViewController.h"
#import "TypeViewController.h"
#import "TravellerViewController.h"
#import "WMPageController.h"

static CGFloat const titleH = 60;
static CGFloat const navBarH = 64;
static CGFloat const maxTitleScale = 1.1;

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
#define YTColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]
@interface CommunityViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
// 选中按钮
@property (nonatomic, weak) UIButton *selTitleButton;

@property (nonatomic, strong) NSMutableArray *buttons;


@end

@implementation CommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
 //   self.view.backgroundColor = [UIColor whiteColor];
    
     [self.navigationController setNavigationBarHidden:YES];
    self.buttons = [NSMutableArray array];
    // 1.创建小scrollview
    [self setupTitleScrollView];
     // 2.创建大scrollview
    [self setupContentScrollView];
    // 3.给大scrollview设置内容vc
    [self addChildViewController];
    // 4.布局按钮标题
    [self setupTitle];
    
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * ScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    
    
    
  }

// 设置头部标题
- (void)setupTitleScrollView
{
    CGRect rect = CGRectMake(0, 0, ScreenW, titleH);
    
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.dk_backgroundColorPicker = DKColorPickerWithRGB(0x139a43,0x4F4F4F);
   // titleScrollView.backgroundColor = YTColor(61, 169, 118);
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
    
}
#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, ScreenW, ScreenH - navBarH);
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    [self.view addSubview:contentScrollView];
    self.contentScrollView = contentScrollView;
}
#pragma mark - 添加子控制器
- (void)addChildViewController
{
    HotViewController *vc = [[HotViewController alloc] init];
    vc.title = @"热议";
    [self addChildViewController:vc];
    
    TypeViewController *vc1 = [[TypeViewController alloc] init];
    vc1.title = @"进入版面";
    [self addChildViewController:vc1];
    
    TravellerViewController *vc2 = [[TravellerViewController alloc] init];
    vc2.title = @"找旅伴";
    [self addChildViewController:vc2];

}
// 布局按钮坐标
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = 100 * WIDTHC;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x+50, 10 * WIDTHC, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:YTColor(143, 216, 193) forState:UIControlStateNormal];
       // btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        // 设置默认第一个被选中
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(3 * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

// 按钮点击
- (void)chick:(UIButton *)btn
{
    // 设置标题颜色为选中颜色
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * ScreenW;
    
    [self setUpOneChildViewController:i];
    
    // 点击按钮切换对应的vc
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}

// 选中按钮
- (void)selTitleBtn:(UIButton *)btn
{
    // 先还原上次选中按钮颜色和大小
    [self.selTitleButton setTitleColor:YTColor(143, 216, 193) forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    // 设置当前选中的
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selTitleButton = btn;
}


//滑动到某个位置就加载相应的位置的vc,不多余创建
- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * ScreenW;
    UIViewController *vc = self.childViewControllers[i];
    //如果当前vc上有子视图就不再添加
    if (vc.view.superview) {
        return;
    }
    // 只是将vc的view加在大scrollview上
    vc.view.frame = CGRectMake(x, 0, ScreenW, ScreenH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}

// 滑动scrollview 改变按钮和vc
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 滑动scrollview得到当前下标
    NSUInteger i = self.contentScrollView.contentOffset.x / ScreenW;
    // 更新选中按钮的颜色和大小
    [self selTitleBtn:self.buttons[i]];
    // 切换vc
    [self setUpOneChildViewController:i];
}
@end

